<?php
$completions = $Users->get_completions($user);

$graded = 0;
$avg = 0;
$sum = 0;
foreach ($completions as &$completion)
{
    if($completion->grade == null)
        $completion->grade = "H";
    else if(is_numeric($completion->grade))
    {
        $avg += $completion->grade;
        ++$graded;
    }
    $sum += $completion->credits;
}
$avg = round($avg / $graded, 2);

?>
<div id="content">
    <h2>Suoritukseni</h2>
    <table class="completions">
        <thead>
            <tr>
                <th class="name">Kurssin nimi</th>
                <th class="grade">Arvosana</th>
                <th class="credits">Opintopisteet</th>
            </tr>
        </thead>
        <tfoot>
            <tr>
                <th class="name">Keskiarvo / Yhteensä</th>
                <th class="grade"><? echo $avg; ?></th>
                <th class="credits"><? echo $sum; ?></th>
            </tr>
        </tfoot>
        <tbody>
            <? foreach ($completions as &$completion) { ?>
                <tr>
                    <td class="name"><? echo e($completion->course); ?></td>
                    <td class="grade"><? echo e($completion->grade); ?></td>
                    <td class="credits"><? echo e($completion->credits); ?></td>
                </tr>
            <? } ?>
        </tbody>
    </table>
</div>
